<?php

namespace common\models\c2\entity;

use common\models\c2\statics\EntityModelStatus;
use Yii;
use yii\helpers\ArrayHelper;

/**
 * This is the model class for table "{{%be_menu}}".
 *
 * @property int $id
 * @property int $parent_id
 * @property string $name
 * @property string $label
 * @property string $path
 * @property string $icon
 * @property int $position
 * @property int $status
 * @property string $created_at
 * @property string $updated_at
 */
class BeMenu extends \common\models\base\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%be_menu}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['parent_id', 'position', 'status'], 'integer'],
            [['name',], 'unique'],
            [['name', 'label', 'path'], 'required'],
            [['created_at', 'updated_at'], 'safe'],
            [['name', 'label'], 'string', 'max' => 30],
            [['path', 'icon'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app.c2', 'ID'),
            'parent_id' => Yii::t('app.c2', 'Parent ID'),
            'name' => Yii::t('app.c2', 'Name'),
            'label' => Yii::t('app.c2', 'Label'),
            'path' => Yii::t('app.c2', 'Path'),
            'icon' => Yii::t('app.c2', 'Icon'),
            'position' => Yii::t('app.c2', 'Position'),
            'status' => Yii::t('app.c2', 'Status'),
            'created_at' => Yii::t('app.c2', 'Created At'),
            'updated_at' => Yii::t('app.c2', 'Updated At'),
        ];
    }

    /**
     * @inheritdoc
     * @return \common\models\c2\query\BeMenuQuery the active query used by this AR class.
     */
    public static function find()
    {
        return new \common\models\c2\query\BeMenuQuery(get_called_class());
    }

    /**
     * setup default values
     **/
    public function loadDefaultValues($skipIfSet = true)
    {
        parent::loadDefaultValues($skipIfSet);
        if ($this->isNewRecord) {
            $this->position = 50;
        }
    }

    public function afterSave($insert, $changedAttributes)
    {
        parent::afterSave($insert, $changedAttributes); // TODO: Change the autogenerated stub
        // if ($insert) {
        //     $this->updateAttributes(['position' => $this->position + $this->id]);
        // }
    }

    /**
     * @return bool
     * @throws \Throwable
     * @throws \yii\db\StaleObjectException
     */
    public function beforeDelete()
    {
        $items = self::findAll(['parent_id' => $this->id]);
        foreach ($items as $item) {
            $item->delete();
        }
        return parent::beforeDelete(); // TODO: Change the autogenerated stub
    }

    public function getParents()
    {
        $items = self::find()->select(['id', 'label'])
            ->orderBy(['position' => SORT_ASC])
            ->where(['parent_id' => 0])
            // ->andFilterWhere(['status' => EntityModelStatus::STATUS_ACTIVE])
            ->asArray()
            ->all();
        $first = ['id' => 0, 'label' => Yii::t('app.c2', 'As root')];
        $root[] = $first;
        if ($this->parent_id === 0 && !$this->isNewRecord) {
            return $root;
        }
        array_unshift($items, $first);
        return $items;
    }

}
